/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.gui.swing;

/**
 * Write about the class here
 *
 * @author Mahbub Mozadded (Sowrov)
 * @date Apr 18, 2010
 */
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class VerticalButton extends JButton {
    private boolean clockwiseRotation;
    /**
     * Create a JButton where text will be shown vertically (top to bottom/clockwise, or bottom to top/anticlockwise)
     * 
     * @param text text
     * @param clockwise true/false
     */
    public VerticalButton(final String text, final boolean clockwise) {
        this.clockwiseRotation = clockwise;
        Font f = getFont();
        FontMetrics fm = getFontMetrics(f);
        int captionHeight = fm.getHeight();
        int captionWidth = fm.stringWidth(text);
        BufferedImage bi = new BufferedImage(captionHeight + 4, captionWidth + 4, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) bi.getGraphics();

        g.setColor(new Color(0, 0, 0, 0)); // transparent
        g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

        g.setColor(getForeground());
        g.setFont(f);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        if (clockwiseRotation) {
            g.rotate(Math.PI / 2);
        } // end if
        else {
            g.rotate(-Math.PI / 2);
            g.translate(-bi.getHeight(), bi.getWidth());
        } // end else

        g.drawString(text, 2, -6);

        Icon icon = new ImageIcon(bi);
        setIcon(icon);

        setMargin(new Insets(15, 2, 15, 2));
        setActionCommand(text);
    } // end VerticalButton()

    public void setText(String text) {
        Font f = getFont();
        FontMetrics fm = getFontMetrics(f);
        int captionHeight = fm.getHeight();
        int captionWidth = fm.stringWidth(text);
        BufferedImage bi = new BufferedImage(captionHeight + 4, captionWidth + 4, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) bi.getGraphics();

        g.setColor(new Color(0, 0, 0, 0)); // transparent
        g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

        g.setColor(getForeground());
        g.setFont(f);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        if (clockwiseRotation) {
            g.rotate(Math.PI / 2);
        } // end if
        else {
            g.rotate(-Math.PI / 2);
            g.translate(-bi.getHeight(), bi.getWidth());
        } // end else

        g.drawString(text, 2, -6);

        Icon icon = new ImageIcon(bi);
        setIcon(icon);

        setMargin(new Insets(15, 2, 15, 2));
        setActionCommand(text);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(
            new Runnable() {
                    public void run() {
                        JFrame frame = new JFrame("Vertical Button Demo");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setLayout(new FlowLayout());
                        frame.add(new VerticalButton("Vertical Up", false));
                        frame.add(new VerticalButton("Vertical Down", true));
                        frame.pack();
                        frame.setVisible(true);
                    } // end run()
                } // end new
        );
    } // end main()
} // end VerticalButton
